package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfMerchPayInfo;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.entity.company.EntityCompanyInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.GetMerchInfoByIdsRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtil;
import com.ocom.payconfig.service.CompanyInfoService;
import com.ocom.payconfig.service.XfMerchPayInfoService;
import com.ocom.payconfig.service.XfMerchantInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MerchInfoController {


    @Autowired
    private XfMerchantInfoService merchantInfoService;

    @Autowired
    CompanyInfoService companyInfoService;

    @Autowired
    XfMerchPayInfoService xfMerchPayInfoService;


    @PostMapping(PayConfigUrl.URLP_PayConfig_MerchPageInfo)
    public Result MerchPageInfo(Long companyId, Integer merchId) {
        EntityXfMerchantInfo entityXfMerchantInfo = merchantInfoService.GetMerchInfoById(companyId, merchId);
        if (merchId == null || merchId == 0) {
            EntityCompanyInfo entityCompanyInfo = companyInfoService.GetCompanyInfo(companyId);

            entityXfMerchantInfo = new EntityXfMerchantInfo();
            entityXfMerchantInfo.setMerchId(0);
            entityXfMerchantInfo.setComId(companyId);
            entityXfMerchantInfo.setParentMerchId(0);
            entityXfMerchantInfo.setMerchOrgin(",");
            entityXfMerchantInfo.setMerchName(entityCompanyInfo.getComName());

        }
        if (entityXfMerchantInfo == null) {
            return ResultUtil.error(ResultEnum.Err_301);
        } else {
            EntityXfMerchPayInfo entityXfMerchPayInfo = new EntityXfMerchPayInfo();
            entityXfMerchPayInfo.setComId(companyId);
            entityXfMerchPayInfo.setMerchId(merchId);
            EntityXfMerchPayInfo entityXfMerchPayInfo1 = xfMerchPayInfoService.getPayInfo(companyId, entityXfMerchPayInfo);
            if (entityXfMerchPayInfo1 != null) {
                entityXfMerchantInfo.setCompanyId(entityXfMerchPayInfo1.getCompanyId());
                entityXfMerchantInfo.setUserName(entityXfMerchPayInfo1.getUserName());
                entityXfMerchantInfo.setPassword(entityXfMerchPayInfo1.getPassword());
            }
            entityXfMerchantInfo.setFeeAccountName(entityXfMerchantInfo.getMerchName());
            return ResultUtil.success(entityXfMerchantInfo);
        }
    }


    // 2.7.获取商户信息(getMerchInfo)
    @GetMapping(PayConfigUrl.URLP_PayConfig_GetMerchInfo)
    public Result GetMerchInfo(Long comId, Long merchId, String merchName, Long merchType, Long parentMerchId, Integer page, Integer size) {
        if (comId == null) {
            return ResultUtil.error(ResultEnum.Err_203);
        }

        List<EntityXfMerchantInfo> entityXfMerchantInfos = merchantInfoService.GetMerchInfo(comId, merchId, merchName, merchType, parentMerchId, page, size);

        if(parentMerchId==null){
            //添加商户
            EntityXfMerchantInfo entityXfMerchantInfo = new EntityXfMerchantInfo();
            entityXfMerchantInfo.setComId(comId);
            //获取单位名
            EntityCompanyInfo entityCompanyInfo = companyInfoService.GetCompanyInfo(comId);
            entityXfMerchantInfo.setMerchName(entityCompanyInfo.getComName());
            if (entityCompanyInfo != null) {
                entityXfMerchantInfo.setMerchName(entityCompanyInfo.getComName());
//            entityXfMerchantInfo.setMerchType(entityCompanyInfo.get)
            }
            entityXfMerchantInfo.setMerchId(0);
            entityXfMerchantInfo.setParentMerchId(-1);
            entityXfMerchantInfo.setMerchOrgin(",");
            entityXfMerchantInfo.setCreateTime(new Date());
            entityXfMerchantInfo.setUpdateTime(new Date());
            entityXfMerchantInfo.setMerchOrgin(",");
            entityXfMerchantInfos.add(entityXfMerchantInfo);
        }


        Map<String, Object> map = new HashMap<>();
        map.put("total", entityXfMerchantInfos.size());
        map.put("list", entityXfMerchantInfos);
        return ResultUtil.success(map);
    }


    @PostMapping(PayConfigUrl.URLP_PayConfig_GetMerchInfoOne)
    public Result GetMerchInfo(@RequestParam("comId") Long comId, @RequestParam("merchId") Integer merchId) {
        EntityXfMerchantInfo entityXfMerchantInfo = merchantInfoService.GetMerchInfo(comId, merchId.longValue());
        if (entityXfMerchantInfo == null) {
            return ResultUtil.error(ResultEnum.Err_2028);
        } else {
            return ResultUtil.success(entityXfMerchantInfo);
        }
    }

//    URLP_PayConfig_GetMerchInfoByIds
    @PostMapping(PayConfigUrl.URLP_PayConfig_GetMerchInfoByIds)
    public Result GetMerchInfoByIds(@RequestBody GetMerchInfoByIdsRequest request) {
       return ResultUtil.success(this.merchantInfoService.getMerchInfoByIds(request.getComId(), request.getIds()));
    }
    // 2.8.判断商户名称是否可用(getMerchInfoByName)
    @GetMapping(PayConfigUrl.URLP_PayConfig_GetMerchInfoByName)
    public Result GetMerchInfoByName(Long comId, String merchName) {
        if (comId == null) {
            return ResultUtil.error(ResultEnum.Err_203);
        }
        List<EntityXfMerchantInfo> entityXfMerchantInfos = merchantInfoService.GetMerchInfoByName(comId, merchName);
        if (entityXfMerchantInfos == null || entityXfMerchantInfos.size() == 0) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(ResultEnum.Err_3044);
        }
    }


    // 2.29.判断商户名称是否可用(existMerchName)
    @PostMapping(PayConfigUrl.URLP_PayConfig_existMerchName)
    public Result existMerchanName(Long comId, Long merchId, String merchName) {
//        System.out.println(comId);
//        System.out.println(merchId);
//        System.out.println(merchName);
        return merchantInfoService.existMerchanName(comId, merchId, merchName);
    }

    // 2.9.添加商户信息(addMerchInfo)
    @PostMapping(PayConfigUrl.URLP_PayConfig_AddMerchInfo)
    public Result AddMerchInfo(@RequestBody @Validated EntityXfMerchantInfo entityXfMerchantInfo) {
        if (entityXfMerchantInfo.getComId() <= 0 || entityXfMerchantInfo.getMerchName().isEmpty() ||
                entityXfMerchantInfo.getParentMerchId() < 0) {
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            merchantInfoService.AddMerchInfo(entityXfMerchantInfo.getComId(), entityXfMerchantInfo);
//            EntityXfMerchPayInfo entityXfMerchPayInfo=new EntityXfMerchPayInfo();
//            entityXfMerchPayInfo.setComId(entityXfMerchantInfo.getComId());
//            entityXfMerchPayInfo.setMerchId(entityXfMerchantInfo.getMerchId());
//            entityXfMerchPayInfo.setCompanyId(entityXfMerchantInfo.getCompanyId());
//            entityXfMerchPayInfo.setUserName(entityXfMerchantInfo.getUserName());
//            entityXfMerchPayInfo.setPassword(entityXfMerchantInfo.getPassword());
//            xfMerchPayInfoService.addPayInfo(entityXfMerchPayInfo.getComId(),entityXfMerchPayInfo);

//            merchantInfoService.UpdateMerchInfo(entityXfMerchantInfo.getComId(), entityXfMerchantInfo);

        } catch (Exception e) {
            e.printStackTrace();
        }


        return ResultUtil.success();
    }

    // 2.10.修改商户信息(updateMerchInfo)
    @PostMapping(PayConfigUrl.URLP_PayConfig_UpdateMerchInfo)
    public Result UpdateMerchInfo(@RequestBody EntityXfMerchantInfo entityXfMerchantInfo) {
        if (entityXfMerchantInfo.getMerchId() < 0 || entityXfMerchantInfo.getComId() <= 0 ||
                entityXfMerchantInfo.getMerchName()==null) {
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            if (entityXfMerchantInfo.getMerchId() != 0) {
                merchantInfoService.UpdateMerchInfo(entityXfMerchantInfo.getComId(), entityXfMerchantInfo);
            }
//            EntityXfMerchPayInfo entityXfMerchPayInfo= new EntityXfMerchPayInfo();
//            entityXfMerchPayInfo.setComId(entityXfMerchantInfo.getComId());
//            entityXfMerchPayInfo.setMerchId(entityXfMerchantInfo.getMerchId());
//            entityXfMerchPayInfo.setCompanyId(entityXfMerchantInfo.getCompanyId());
//            entityXfMerchPayInfo.setUserName(entityXfMerchantInfo.getUserName());
//            entityXfMerchPayInfo.setPassword(entityXfMerchantInfo.getPassword());
//            EntityXfMerchPayInfo entityXfMerchPayInfo1 = xfMerchPayInfoService.getPayInfo(entityXfMerchantInfo.getComId(),entityXfMerchPayInfo);
//            if(entityXfMerchPayInfo1==null){
//                xfMerchPayInfoService.addPayInfo(entityXfMerchantInfo.getComId(),entityXfMerchPayInfo);
//            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.Err_3014);
        }
        return ResultUtil.success();
    }


    @PostMapping(PayConfigUrl.URLP_PayConfig_MerchPageOne)
    public Result MerchPageOne(Long companyId, Long parentMerchId, Long merchId) {
        EntityXfMerchantInfo entityXfMerchantInfo = merchantInfoService.GetMerchInfoById(companyId, parentMerchId, merchId);
        if (entityXfMerchantInfo == null) {
            return ResultUtil.error(ResultEnum.Err_301);
        } else {
            return ResultUtil.success(entityXfMerchantInfo);
        }
    }

    @PostMapping(PayConfigUrl.URLP_PayConfig_MerchPageAddPayInfo)
    public Result addPayInfo(@RequestBody EntityXfMerchPayInfo entityXfMerchPayInfo) {
        try {
            xfMerchPayInfoService.addPayInfo(entityXfMerchPayInfo.getComId(), entityXfMerchPayInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(ResultEnum.Err_2016);
        }
        return ResultUtil.success();
    }

//    // (GetMerchInfoByMachNo)
//    @PostMapping(PayConfigUrl.URLP_PayConfig_GetMerchInfoByMachNo)
//    public Result GetMerchInfoByMachNo(Long comId, String machNo){
//        if (comId == null ){
//            return ResultUtil.error(ResultEnum.Err_203);
//        }
//        return  merchantInfoService.GetMerchInfoByMachNo(comId,machNo);
//    }

    /**
     * 增加缴费账户
     * @param entityXfMerchantInfo
     * @return
     */
    @PostMapping(PayConfigUrl.Url_FeeAccountAdd)
    public Result addFeeAccount(@RequestBody @Validated EntityXfMerchantInfo entityXfMerchantInfo) {
        if(StringUtil.isNotBlank(entityXfMerchantInfo.getFeeAccountName())){
            entityXfMerchantInfo.setMerchName(entityXfMerchantInfo.getFeeAccountName());
        }
//        System.out.println("getFeeAccountName==="+entityXfMerchantInfo.getFeeAccountName());
        if (entityXfMerchantInfo.getComId() <= 0 || StringUtil.isEmpty(entityXfMerchantInfo.getMerchName())) {
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            if(merchantInfoService.exitsMerchantName(entityXfMerchantInfo.getComId(),entityXfMerchantInfo.getMerchName()) != null){
                return ResultUtil.error(ResultEnum.Err_3024);
            }
            Boolean bReturn = merchantInfoService.AddXfMerchantInfo(entityXfMerchantInfo.getComId(), entityXfMerchantInfo);
            return ResultUtil.success(bReturn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *获取所有可用缴费账户
     * @param comId
     * @return
     */
    @PostMapping(PayConfigUrl.URL_GetAllAccount)
    public Result getAllAccount(Long comId){
        return merchantInfoService.getAllAccount(comId);
    }

    /**
     * 缴费账户列表
     * @param comId
     * @return
     */
    @PostMapping(PayConfigUrl.URL_FeeAccountList)
    public Result getFeeAccountList(Long comId){

        return ResultUtil.success(merchantInfoService.getFeeAccountList(comId));
    }

    /**
     * 缴费商户列表
     * @param comId
     * @return
     */
    @PostMapping(PayConfigUrl.URL_FeeMerchList)
    public Result getFeeMerchList(Long comId){
        return ResultUtil.success(merchantInfoService.getFeeMerchList(comId));
    }

    /**
     * 15.5修改缴费账户
     * @param request
     * @return
     */
    @PostMapping(PayConfigUrl.URL_updateFeeAccount)
    public Result updateFeeAccount(@RequestBody EntityXfMerchantInfo request){
        if(StringUtil.isNotBlank(request.getFeeAccountName())){
            request.setMerchName(request.getFeeAccountName());
        }
        return merchantInfoService.updateMerchInfo(request.getComId(),request);
    }

    /**
     * 15.3改变商户缴费状态
     * @param request
     * @return
     */
    @PostMapping(PayConfigUrl.URL_changeMerchStatus)
    public Result changeMerchStatus(@RequestBody EntityXfMerchantInfo request){
        return merchantInfoService.changeMerchStatus(request.getComId(),request.getMerchId());
    }


}
